package org.zk.dubbo_consumer.controller;


import cn.hutool.http.HttpResponse;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zk.entity.TeacherBill;
import org.zk.entity.Tixian;
import org.zk.service.AdminBillService;
import org.zk.service.TeacherBillService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@Controller
public class TeacherBillController {
    @Resource
    private TeacherBillService teacherBillService;

    @RequestMapping("/getMoneyRecord")
    public ModelAndView toRecord(int teacherId,HttpServletResponse response){
       List<TeacherBill> teacherBills=teacherBillService.getTeacherBill(teacherId);
        if(teacherBills.size()==0){
            response.setContentType("text/html; charset=UTF-8"); //转码
            PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            out.flush();
            out.println("<script>");
            out.println("alert('您账户下的零钱暂时没有明细！');");
            out.println("history.back();");
            out.println("</script>");
        }
        ModelAndView mv=new ModelAndView("teacher/moneyRecord");
        mv.addObject("id",teacherId);
        return mv;
    }

    /**
     * 教师交易记录
     * @param id
     * @param pageSize
     * @param pageNum
     * @param status
     * @param start
     * @param end
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTBillByTid")
    public Map<String ,Object> getTeacherBillPage(  String id,String pageSize,String pageNum,String status,String start,String end)  {
        Map map=new HashMap();
        if(!start.equals("")){
            start+= " 00:00:00";
        }
        if(!end.equals("")){
            end+=" 23:59:59";
        }
        map.put("teacherid",id);
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("status",status);
        map.put("start",start);
        map.put("end",end);
        PageInfo page=teacherBillService.getTeacherbillsPage(map);
        List<TeacherBill> tbid=page.getList();
        List<TeacherBill> lq=page.getList();
        for (int i = 0; i < lq.size(); i++) {
         String flag=lq.get(i).getTeacherBillStatus();
            if (flag.equals("0")){
                lq.get(i).setTeacherBillStatus("提现");
            }
            if (flag.equals("1")){
                lq.get(i).setTeacherBillStatus("收入");
            }
        }
        map.put("page",page);
        map.put("total",page.getTotal());
        map.put("pages",page.getPages());
        return map;
    }


    @RequestMapping("/delTeacherBill")
    @ResponseBody
    public int delTeacherBill(int id){
       int result= teacherBillService.delTeacherBill(id);
       return result;
    }
}

